package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.ConvolveOp;
import java.util.ArrayList;

import javax.sound.sampled.Line;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;



public class PathMatrixPanel extends JPanel
{
	JButton[][] panels;
	
	public PathMatrixPanel()
	{
		panels  = new JButton[15][15];
		initUI();
	}
	
	public void initUI()
	{
		GridLayout gl = new GridLayout(16, 16, 1, 1);
		setLayout(gl);
		Dimension d = new Dimension(250,250);
		setSize(d);
		setPreferredSize(d);
		setBorder(new TitledBorder("Path Matrix"));
		paintButtons();		
	}
	
	public void updatePathMatrix(int dimension)
	{	
		for(int i = 0; i < dimension; i++)
		{
			for(int j = 0; j < dimension; j++)
			{
				if(i != j)
				{
					final int a = i;
					final int b = j;

					panels[i][j].setEnabled(true);
					panels[i][j].setBackground(Color.WHITE);
					panels[i][j].setText("0");
					
				}
				else
				{
					panels[i][j].setText("1");
					panels[i][j].setEnabled(true);
					panels[i][j].setBackground(Color.ORANGE);
				}
			}
		}
	
		for(int i = dimension; i < panels.length; i++)
		{
			int count = 0;
			Color c = new Color(238,238,238);
			
			while(count < panels.length)
			{
					panels[i][count].setBackground(c);
					panels[i][count].setText(null);
					panels[i][count].setEnabled(false);
			
				count++;
			}
			for(int j = dimension; j < panels.length; j++)
			{
				count = 0;
				while(count < panels.length)
				{
						panels[count][j].setBackground(c);
						panels[count][j].setText(null);
						panels[count][j].setEnabled(false);
					count++;
				}
				
				if(i != j)
				{
					panels[i][j].setBackground(c);
					panels[i][j].setText(null);
					panels[i][j].setEnabled(false);
				}
			}
		}
		
	}
	
	public void updatePathMatrixOutside(int dimension, int[][] matrix)
	{	
		for(int i = 0; i < dimension; i++)
		{
			for(int j = 0; j < dimension; j++)
			{
				if(i != j && matrix[i][j] == 0)
				{
					final int a = i;
					final int b = j;

					panels[i][j].setEnabled(true);
					panels[i][j].setBackground(Color.WHITE);
					panels[i][j].setText("0");
					
				}
				else
				{
					panels[i][j].setText("1");
					panels[i][j].setEnabled(true);
					panels[i][j].setBackground(Color.ORANGE);
				}
			}
		}
	
		for(int i = dimension; i < panels.length; i++)
		{
			int count = 0;
			Color c = new Color(238,238,238);
			
			while(count < panels.length)
			{
					panels[i][count].setBackground(c);
					panels[i][count].setText(null);
					panels[i][count].setEnabled(false);
			
				count++;
			}
			for(int j = dimension; j < panels.length; j++)
			{
				count = 0;
				while(count < panels.length)
				{
						panels[count][j].setBackground(c);
						panels[count][j].setText(null);
						panels[count][j].setEnabled(false);
					count++;
				}
				
				if(i != j)
				{
					panels[i][j].setBackground(c);
					panels[i][j].setText(null);
					panels[i][j].setEnabled(false);
				}
			}
		}
		
	}

	
	public void paintButtons()
	{
		for(int i = 0; i < panels.length; i++)
		{
			JButton p = new JButton(String.format("%02d", i + 1));
			p.setBorder(null);
			p.setBackground(new Color(238,238,238));
			panels[0][i] = p;
		}
		add(new JLabel());
		
		for(int i = 0; i < panels.length; i++)
		{	
			add(panels[0][i]);
		}
		for (int i = 0; i < panels.length; i++) 
		{
			JButton p = new JButton(String.format("%02d", i + 1));
			p.setBorder(null);
			p.setBackground(new Color(238,238,238));
			panels[i][0] = p;
			add(panels[i][0]);
			
			for (int j = 0; j < panels[i].length; j++) 
			{
				JButton p1 = new JButton();
				p1.setBorder(new EtchedBorder());
				p1.setBackground(new Color(238,238,238));
				p1.setEnabled(false);
				panels[i][j] = p1;
				if(i == j && i < 3)
					{
					panels[i][j].setEnabled(true);
					panels[i][j].setText("0");
					}
				          
				add(panels[i][j]);
			}
		}		
		updatePathMatrix(3);
	}

	
}


